﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DataAccess
{
    public class NhapKhoDataTable
    {
        private DataTable dt = new DataTable();
        public NhapKhoDataTable()
        {
            dt.Columns.Add("MaSanPham");
            dt.Columns.Add("TenSanPham");
            dt.Columns.Add("SoLuong");
            dt.Columns.Add("GiaSanPham");
            dt.Columns.Add("VAT");
            dt.Columns.Add("TongTien");
            dt.Columns.Add("NgayNhap");
            dt.Columns.Add("MaNhanVien");
            dt.Columns.Add("MaDonVi");
            dt.Columns.Add("TenDonVi");
            dt.Columns.Add("GhiChu");
            dt.Columns.Add("MaNhaCungCap");
            dt.Columns.Add("TenNhaCungCap");
            dt.Columns.Add("MaNhomSanPham");
            dt.Columns.Add("TenNhomSanPham");

        }

        public DataTable GetTable()
        {
            return dt;
        }

        public DataTable InsertUpdate(string masanpham, string tensanpham, int soluong, double giasanpham,
            double vat, string ngaynhap, int manhanvien, int madonvi, string tendonvi, string ghichu,
            int manhomsanpham,string tennhomsanpham,int manhacungcap,string tennhacungcap)
        {

            bool kiemTra = false;
            if (dt.Rows.Count != 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    if (row["masanpham"].ToString() == masanpham)
                    {
                        row["MaSanPham"] = masanpham;
                        row["TenSanPham"] = tensanpham;
                        row["SoLuong"] = soluong;
                        row["GiaSanPham"] = giasanpham;
                        row["VAT"] = vat;
                        row["TongTien"] = (vat == 0 ? (soluong * giasanpham) : ((soluong * giasanpham) + (soluong * giasanpham) * (vat / 100)));
                        row["NgayNhap"] = ngaynhap;
                        row["MaNhanVien"] = manhanvien;
                        row["MaDonVi"] = madonvi;
                        row["TenDonVi"] = tendonvi;
                        row["GhiChu"] = ghichu;
                        row["MaNhomSanPham"] = manhomsanpham;
                        row["TenNhomSanPham"] = tennhomsanpham;
                        row["MaNhaCungCap"] = manhacungcap;
                        row["TenNhaCungCap"] = tennhacungcap;
                        kiemTra = true;
                    }
                }
                if (!kiemTra)
                {
                    DataRow row = dt.NewRow();
                    row["MaSanPham"] = masanpham;
                    row["TenSanPham"] = tensanpham;
                    row["SoLuong"] = soluong;
                    row["GiaSanPham"] = giasanpham;
                    row["VAT"] = vat;
                    row["TongTien"] = (vat == 0 ? (soluong * giasanpham) : ((soluong * giasanpham) + (soluong * giasanpham) * (vat / 100)));
                    row["NgayNhap"] = ngaynhap;
                    row["MaNhanVien"] = manhanvien;
                    row["MaDonVi"] = madonvi;
                    row["TenDonVi"] = tendonvi;
                    row["GhiChu"] = ghichu;
                    row["MaNhomSanPham"] = manhomsanpham;
                    row["TenNhomSanPham"] = tennhomsanpham;
                    row["MaNhaCungCap"] = manhacungcap;
                    row["TenNhaCungCap"] = tennhacungcap;
                    dt.Rows.Add(row);
                }
            }
            else
            {
                DataRow row = dt.NewRow();
                row["MaSanPham"] = masanpham;
                row["TenSanPham"] = tensanpham;
                row["SoLuong"] = soluong;
                row["GiaSanPham"] = giasanpham;
                row["VAT"] = vat;
                row["TongTien"] = (vat == 0 ? (soluong * giasanpham) : ((soluong * giasanpham)+ (soluong * giasanpham) * (vat / 100)));
                row["NgayNhap"] = ngaynhap;
                row["MaNhanVien"] = manhanvien;
                row["MaDonVi"] = madonvi;
                row["TenDonVi"] = tendonvi;
                row["GhiChu"] = ghichu;
                row["MaNhomSanPham"] = manhomsanpham;
                row["TenNhomSanPham"] = tennhomsanpham;
                row["MaNhaCungCap"] = manhacungcap;
                row["TenNhaCungCap"] = tennhacungcap;
                dt.Rows.Add(row);

            }
            return dt;
        }

        public DataTable Delete(string id)
        {
            foreach (DataRow row in dt.Rows)
            {
                if (row["MaSanPham"].ToString() == id)
                {
                    dt.Rows.Remove(row);
                    break;
                }
            }
            return dt;
        }

        public DataRow View(string id)
        {
            foreach (DataRow row in dt.Rows)
            {
                if (row["MaSanPham"].ToString() == id)
                {
                    return row;
                }
            }
            return null;
        }


        public double GetTotalMoney()
        {
            double money = 0;
            if (dt.Rows.Count != 0)
            {
                foreach (DataRow rows in dt.Rows)
                {
                    money += double.Parse(rows["TongTien"].ToString());
                }
            }
            return money;
        }

        public double GetTotalMoneyVAT()
        {
            double money = 0;
            if (dt.Rows.Count != 0)
            {
                foreach (DataRow rows in dt.Rows)
                {
                    double vat = int.Parse(rows["SoLuong"].ToString()) * double.Parse(rows["GiaSanPham"].ToString()) * (double.Parse(rows["VAT"].ToString()) / 100);
                    money += vat;
                }
            }
            return money;
        }

        public int GetTotalCount()
        {
            int count = 0;
            if (dt.Rows.Count != 0)
            {
                count = dt.Rows.Count;
            }
            return count;
        }

    }
}
